<?php

class Users_Model_Access_Privilege_Storage_Table extends Workset_Model_Object_Storage_Table_Abstract {

	protected $_objectClass 		= 'Users_Model_Access_Privilege';

	protected $_tableClass 			= 'Users_Table_Access_Privilege';

	protected $_iteratorClass 		= 'Workset_Model_Object_Iterator';

	protected $_linkedObjectTableClass = 'Users_Table_Access_Privilege2Resource';

	function linkToObject(Workset_Model_Object_Interface $object, Workset_Model_Object_Interface $refObject) {
        $row = $object->getData();
        if ($row instanceof Workset_Db_Table_Row) {
            return $row->addDependentRow($this->_linkedObjectTableClass, array(
                'resource_id' => $refObject->getId(),
            ));
        }
    }

    function unlinkObject(Workset_Model_Object_Interface $object, Workset_Model_Object_Interface $refObject) {
        $row = $object->getData();
        if ($row instanceof Workset_Db_Table_Row) {
            $subrow = $row->findDependentRow($this->_linkedObjectTableClass, 'privilege', array(
                'resource_id = ?' => $refObject->getId(),
            ));
            if ($subrow) {
                return $subrow->delete();
            }
        }
    }

    function getAllByResource(Workset_Model_Object_Interface $resource, $where = null, $order = null, $limit = null, $offset = null, $offsetByPage = false) {
        $row = $resource->getData();
        if ($row instanceof Workset_Db_Table_Row) {

            if (true == $offsetByPage) {
                $offset = $this->getPageOffset($limit, $offset);
            }
            
			$table = $row->getTable();
			$select = $table->prepareSelect($where, $order, $limit, $offset);

			$rowset = $row->findManyToManyRowset($this->_tableClass, $this->_linkedObjectTableClass, 'resource', 'privilege', $select);

            return $this->getIterator($rowset, array(
                'countRows' => $this->_countAllRecords,
                'filter' => $select
            ));
		}
    }

    function getAvailablePrivileges(Workset_Model_Object_Interface $resource, $where = null, $order = null, $limit = null, $offset = null, $offsetByPage = false) {

        if (true == $offsetByPage) {
            $offset = $this->getPageOffset($limit, $offset);
        }

		$table = $this->_getTable();
	    $select = $table->prepareSelect($where, $order, $limit, $offset);

        $iselect = $this->_getTable()->select();

        $iselect->from(
            array('m' => $this->_getTable()->info(Zend_Db_Table_Abstract::NAME)),
            array('id')
        )
       ->join(
            array('i' => $this->_getTable($this->_linkedObjectTableClass)->info(Zend_Db_Table_Abstract::NAME)),
            "i.privilege_id = m.id",
            array()
        )
        ->where(
            'i.resource_id = ?', $resource->getId()
        );

        $select->where("id not in(?)", new Zend_Db_Expr($iselect->assemble()));

        $rowset = $table->fetchAll($select);

        return $this->getIterator($rowset, array(
            'countRows' => $this->_countAllRecords,
            'filter' => $select
        ));

    }

}

?>